package com.tiancheng.trade.settlement.task;

import com.tiancheng.trade.settlement.exception.ApplicationException;
import com.tiancheng.trade.settlement.service.settlement.ISettlementRuleService;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 清分定时任务
 *
 * @author lzh
 */
@Component
@Slf4j
public class SettlementRuleTask {

    @Autowired
    ISettlementRuleService settlementService;

    /**
     * 定时生效
     *
     * @return
     */
//    @Scheduled(initialDelay = 60000, fixedRate = 60000)
    @XxlJob("autoEffective")
    public ReturnT<String> autoEffective(String param) throws ApplicationException {
        log.info("定时生效清分规则 开启");
        try {
            int cnt = settlementService.autoEffective();
            if (cnt == 1) {
                log.info("定时生效清分规则成功");
            } else if (cnt == 0) {
                log.info("无定时生效数据");
            } else {
                throw new Exception("定时生效清分规则失败");
            }
        } catch (Exception e) {
            log.error("定时生效清分规则 is fail", e);
        }
        log.info("定时生效清分规则 结束");
        return ReturnT.SUCCESS;
    }

    /**
     * 定时失效
     *
     * @return
     */
//    @Scheduled(initialDelay = 60000, fixedRate = 50000)
    @XxlJob("autoExpire")
    public ReturnT<String> autoExpire(String param) throws ApplicationException {
        log.info("定时失效清分规则 开启");
        try {
            int cnt = settlementService.autoExpired();
            if (cnt == 1) {
                log.info("定时失效清分规则成功");
            } else if (cnt == 0) {
                log.info("无定时失效数据");
            } else {
                throw new Exception("定时失效失败");
            }
        } catch (Exception e) {
            log.error("定时失效清分规则 is fail", e);
        }
        log.info("定时失效清分规则 结束");
        return ReturnT.SUCCESS;
    }

}
